<template>
    <div>
        <div class="sysDsConf-container">
            <el-container>
                <el-main>
                    <el-row>
                        <el-form :inline="true" size="small" :model="sysDsConfFormInline" class="demo-form-inline" @keyup.enter.native="sysDsConfQuery" @submit.native.prevent>
                            <el-form-item>
                                <el-button type="primary" icon="el-icon-plus" v-if="permissions.indexOf('dsConf_sysDsConf_add') > -1" @click="sysDsConfOpenCreateDialog">添加</el-button>

                            </el-form-item>
                            <el-form-item>
                                <el-input v-model="sysDsConfPageData.query.dsName" placeholder="数据源名称"/>
                            </el-form-item>
                            <el-form-item>
                                <el-select  :disabled="true" v-model="sysDsConfPageData.query.dsType"  filterable placeholder="请选择">
                                    <el-option
                                            v-for="item in dsTypeOptions"
                                            :key="item.key"
                                            :label="item.value"
                                            :value="item.key">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item>
                                <el-button icon="el-icon-search" type="primary" @click="sysDsConfQuery">查询</el-button>
                                <el-button class="margin-left-10" icon="el-icon-delete" @click="sysDsConfReset">清空</el-button>
                            </el-form-item>
                            <el-tooltip effect="dark" content="刷新" placement="top-start">
                                <el-button size="small"  style="float: right;" icon="el-icon-refresh" @click="sysDsConfQuery" circle></el-button>
                            </el-tooltip>
                        </el-form>
                    </el-row>
                    <el-table
                            border
                            stripe
                            size="mini"
                            :data="sysDsConfPageData.pageInfo.records"
                            highlight-current-row
                            ref="sysDsConfTable">
                        <el-table-column label="序号" width="80" align="center">
                            <template slot-scope="scope">
                                <span>{{scope.$index+(sysDsConfPageData.pageInfo.current - 1) * sysDsConfPageData.pageInfo.size + 1}}</span>
                            </template>
                        </el-table-column>
                        <el-table-column prop="dsName" label="数据源名称" align="center" show-overflow-tooltip/>
                        <el-table-column prop="dsType" label="数据源类型"  :formatter="dsTypeFormat" align="center" show-overflow-tooltip/>
                        <el-table-column prop="dsUrl" label="数据源url" align="center" show-overflow-tooltip/>
                        <el-table-column prop="dsUsername" label="数据源账号" align="center" show-overflow-tooltip/>
                        <el-table-column prop="dsPasswd" label="数据源密码" align="center" show-overflow-tooltip/>
                        <el-table-column prop="createTime" label="创建时间" align="center" show-overflow-tooltip/>
                        <el-table-column label="操作" width="200px" align="center">
                            <template slot-scope="scope">
                                <el-button type="text" size="small" icon="el-icon-view" @click="lookSysDsConf(scope.row)">查看</el-button>
                                <el-button type="text" size="small" icon="el-icon-edit" @click="sysDsConfOpenUpdateDialog(scope.row)" v-if="permissions.indexOf('dsConf_sysDsConf_edit') > -1">修改</el-button>
                                <el-button type="text" size="small" icon="el-icon-delete" @click="sysDsConfDeleteRecord(scope.row)" v-if="permissions.indexOf('dsConf_sysDsConf_del') > -1">删除</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-row type="flex" justify="end" v-show="sysDsConfPageData.pageInfo.total>0">
                        <el-pagination class="pageBar"
                                       background
                                       @size-change="sysDsConfSizeChange"
                                       @current-change="sysDsConfCurrentChange"
                                       :current-page="sysDsConfPageData.pageInfo.pageNum"
                                       :page-sizes="[5, 10, 20, 30, 50]"
                                       :page-size="sysDsConfPageData.pageInfo.pageSize"
                                       layout="total, sizes, prev, pager, next, jumper"
                                       :total="sysDsConfPageData.pageInfo.total">
                        </el-pagination>
                    </el-row>
                </el-main>
            </el-container>
            <el-dialog
                    customClass="sysDsConfIndex"
                    :title="sysDsConfDialogData.sysDsConfTitle"
                    :visible.sync="sysDsConfDialogData.visible"
                    width="600px"
                    append-to-body="true"
                    @close="cancelSysDsConfDialog()">
                <el-container>
                    <el-main>
                        <el-form ref="sysDsConfForm" :rules="sysDsConfRules" :model="sysDsConfDialogData.sysDsConfFormData"
                                 label-width="80px">
                            <el-form-item label="名称" ref="dsName" prop="dsName">
                                <el-input :disabled="isSysDsConfLook" v-model="sysDsConfDialogData.sysDsConfFormData.dsName"/>
                            </el-form-item>
                            <el-form-item label="类型" :disabled="isSysDsConfLook" ref="dsType" prop="dsType">
                                <el-select :disabled="true" v-model="sysDsConfDialogData.sysDsConfFormData.dsType" filterable placeholder="请选择">
                                    <el-option
                                            v-for="item in dsTypeOptions"
                                            :key="item.key"
                                            :label="item.value"
                                            :value="item.key">
                                    </el-option>
                                </el-select>
                            </el-form-item>
                            <el-form-item label="url" ref="dsUrl" prop="dsUrl">
                                <el-input :disabled="isSysDsConfLook" v-model="sysDsConfDialogData.sysDsConfFormData.dsUrl"/>
                            </el-form-item>
                            <el-form-item label="账号" ref="dsUsername" prop="dsUsername">
                                <el-input :disabled="isSysDsConfLook" v-model="sysDsConfDialogData.sysDsConfFormData.dsUsername"/>
                            </el-form-item>
                            <el-form-item label="密码" ref="dsPasswd" prop="dsPasswd">
                                <el-input :disabled="isSysDsConfLook" v-model="sysDsConfDialogData.sysDsConfFormData.dsPasswd"/>
                            </el-form-item>
                        </el-form>
                    </el-main>
                </el-container>
                <div slot="footer" class="dialog-footer"  v-show="!isSysDsConfLook">
                    <el-button @click="sysDsConfSaveOrUpdate" type="primary">确 定</el-button>
                    <el-button @click="cancelSysDsConfDialog()">取 消</el-button>
                </div>
            </el-dialog>
        </div>
    </div>
</template>

<script>

    import sysDsConfApi from "./api/sysDsConf.service"

    export default {
        components: {},
        data() {
            return {
                dsTypeOptions:[{
                    key: "",
                    value: '全部'
                },{
                    key: 1,
                    value: 'MySQL数据库'
                },{
                    key: 2,
                    value: '达梦数据库'
                },{
                    key: 3,
                    value: '瀚高数据库'
                },{
                    key: 4,
                    value: '金仓数据库'
                }],
                dsCfgs:[{
                    key: "",
                    value: '全部'
                },{
                    key: 1,
                    value: 'tpf'
                },{
                    key: 2,
                    value: 'tpf_config'
                },{
                    key: 3,
                    value: 'test'
                }],
                sysDsConfRules: {
                    dsName: [
                        {
                            required: true,
                            message: '名称不能为空！'
                        },
                    ],
                    dsType: [
                        {
                            required: true,
                            message: '类型不能为空！'
                        },
                    ],
                    dsUrl: [
                        {
                            required: true,
                            message: 'url不能为空！'
                        },
                    ],
                    dsUsername: [
                        {
                            required: true,
                            message: '账号不能为空！'
                        },
                    ],
                    dsPasswd: [
                        {
                            required: true,
                            message: '密码不能为空！'
                        },
                    ],
                },
                permissions: JSON.parse(sessionStorage.getItem('permissions')),
                sysDsConfFormInline: {},
                isSysDsConfLook:false,
                sysDsConfDialogData: {
                    visible: false,
                    sysDsConfTitle: "",
                    sysDsConfFormData: {
                        dsName: "",
                        dsType: 1,
                        dsUrl: "",
                        dsUsername: "",
                        dsPasswd: "",
                    },
                },
                sysDsConfPageData: {
                    query: {
                        dsName: "",
                        dsType: 1,
                        dsCfg: "tpf",
                    },
                    pageInfo: {
                        size: 10,
                        current: 1,
                        total: 0
                    }
                },
            };

        },
        created() {
            this.getDSName();
            this.sysDsConfQuery();
        },
        methods: {

            /**
             * 数据源类型转码
             */
            dsTypeFormat: function (row, column) {
                let dsType;

                switch (row.dsType) {
                    case 1:
                        dsType = "MySql数据库";
                        break
                    case 2:
                        dsType = "达梦数据库";
                        break
                    case 3:
                        dsType = "瀚高数据库";
                        break
                    case 4:
                        dsType = "金仓数据库";
                        break
                    default:
                        dsType = "未知数据库"
                }
                return dsType
            },

            getDSName() {
                sysDsConfApi.getDSName().then(res => {
                    this.dsCfgs = res.data;
                })
            },

            /**
             * 备注查询
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            sysDsConfQuery() {

                let _this = this;
                sysDsConfApi.getSysDsConfPage(
                    Object.assign({
                        current: this.sysDsConfPageData.pageInfo.current,
                        size: this.sysDsConfPageData.pageInfo.size
                    },this.sysDsConfPageData.query)

                ).then((res) => {
                    _this.sysDsConfPageData.pageInfo = res.data;
                })
            },
            /**
             * 备注查询重置
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            sysDsConfReset() {
                this.sysDsConfPageData.query.dsName = "";
                this.sysDsConfPageData.query.dsType = "";
                this.sysDsConfQuery();
            },

            /**
             * 备注页码变化
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            sysDsConfSizeChange(val) {
                this.sysDsConfPageData.pageInfo.size = val;
                this.sysDsConfQuery();
            },

            /**
             * 备注当前页
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            sysDsConfCurrentChange(val) {
                this.sysDsConfPageData.pageInfo.current = val;
                this.sysDsConfQuery();
            },

            /**
             * 备注添加
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            sysDsConfOpenCreateDialog() {
                this.sysDsConfDialogData.sysDsConfTitle = "添加";
                this.sysDsConfDialogData.visible = true;
                this.sysDsConfDialogData.sysDsConfFormData = {
                    dsName: "",
                    dsType: 1,
                    dsUrl: "",
                    dsUsername: "",
                    dsPasswd: "",
                };
            },

            /**
             * 备注修改
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            sysDsConfOpenUpdateDialog(row) {
                this.sysDsConfDialogData.sysDsConfTitle = "修改";
                this.sysDsConfDialogData.visible = true;
                let _this = this;
                sysDsConfApi.getSysDsConfById(row.id).then((res) => {
                    _this.sysDsConfDialogData.sysDsConfFormData = res.data;
                });
            },

            /**
             * 备注保存
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            sysDsConfSaveOrUpdate() {
                let _this = this;
                this.$refs["sysDsConfForm"].validate(valid => {
                    if (valid) {
                        if (this.sysDsConfDialogData.sysDsConfFormData.id != null) {
                            sysDsConfApi.updateSysDsConfById( this.sysDsConfDialogData.sysDsConfFormData).then((res) => {
                                if (res.code == 10200) {
                                    this.$message.success("修改成功！");
                                    _this.sysDsConfDialogData.visible = false;
                                    _this.sysDsConfQuery();
                                } else {
                                    _this.$alert(res.status, '提示', {
                                        confirmButtonText: '确定',
                                        callback: action => {
                                        }
                                    });
                                }
                            });
                        } else {
                            sysDsConfApi.saveSysDsConf(this.sysDsConfDialogData.sysDsConfFormData).then((res) => {
                                if (res.code == 10200) {
                                    this.$message.success("添加成功！");
                                    _this.sysDsConfDialogData.visible = false;
                                    _this.sysDsConfQuery();
                                } else {
                                    _this.$alert(res.status, '提示', {
                                        confirmButtonText: '确定',
                                        callback: action => {
                                        }
                                    });
                                }
                            });
                        }

                    }else {
                        return false;
                    }
                });
            },

            /**
             * 备注查看
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            lookSysDsConf(row) {
                this.sysDsConfDialogData.visible = true;
                this.sysDsConfDialogData.sysDsConfTitle = "查看";
                this.isSysDsConfLook = true;
                this.sysDsConfDialogData.sysDsConfFormData = JSON.parse(JSON.stringify(row));
            },

            /**
             * 备注关闭
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            cancelSysDsConfDialog() {
                let obj = {
                    dsName: "",
                    dsType: "",
                    dsUrl: "",
                    dsUsername: "",
                    dsPasswd: "",
                };
                this.sysDsConfDialogData.sysDsConfFormData = obj;
                //sysDsConfForm
                this.$refs["sysDsConfForm"].resetFields();
                this.sysDsConfDialogData.visible = false
                this.isSysDsConfLook = false;

            },

            /**
             * 备注删除
             * @author wangwuhui
             * @date 2020-07-10 11:55:40
             */
            sysDsConfDeleteRecord(data) {
                if (data.id <= 0) {
                    this.$alert('请先选择一条要删除的记录！', '提示', {
                        confirmButtonText: '确定',
                        callback: action => {
                        }
                    });
                    return;
                }

                this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(()=>{
                    sysDsConfApi.deleteSysDsConf(data.id).then((res) => {
                        if (res.data > 0) {
                            this.$message({
                                type: 'success',
                                message: '备注删除成功!'
                            });
                            this.sysDsConfQuery();
                        } else {
                            this.$message({
                                type: 'error',
                                message: '备注删除失败!'
                            });
                        }

                    });
                });
            },

        }
    };
</script>
